﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class WakeUpConfigBomb : WakeUpConfigStartPosAndOri {

    public float Volume = 0;
    public WakeUpConfigBomb(Vector2 pos ,Vector2 ori,float volume):base(pos,ori) {
        this.Volume = volume;
    }

}
public class Bomb : PrefabCommonBase ,ISparker
{
    public Material MaterialDefault = null;
    public Material MaterialSpark = null;

    private SpriteRenderer spr_ = null;

    public float BombExistDurtion = 0.5f;
    public float BombFq = 0.1f;

    public override void SetStatusByConfig(WakeUpConfigBase config)
    {
        WakeUpConfigBomb wc = config as WakeUpConfigBomb;
        if (wc != null)
        {
            this.transform.localPosition = new Vector3(wc.StartPos.x, wc.StartPos.y, this.transform.localPosition.z);
            if (wc.Ori.x * wc.Ori.x < 1) {
                this.transform.localEulerAngles = new Vector3(
                this.transform.localEulerAngles.x,
                this.transform.localEulerAngles.y,
                MathHelper.OriToDegree(wc.Ori)
                );
            }
        }

        if (this.spr_ == null)
        {
            this.spr_ = this.GetComponent<SpriteRenderer>();
        }

        var ap = this.GetComponent<ActionProcesser>();
        if (ap != null) {
            ap.StopAllActions();
            ap.RunAction(new ActionSpark(this, BombExistDurtion, BombFq));
            ap.RunAction(new ActionShake(this.transform, wc.Volume, BombExistDurtion));
            // if (wc.Volume >0.1) {
            //     CameraMgr.GetInstance().Shake(BombExistDurtion,0.5f,2);
            // }
        }

        
    }

    public void TurnNormal()
    {
        if (this.spr_ != null && this.MaterialDefault != null) {
            this.spr_.material = this.MaterialDefault;
        }
    }

    public void TurnSpark()
    {
        if (this.spr_ != null && this.MaterialSpark != null)
        {
            this.spr_.material = this.MaterialSpark;
        }
    }

    protected override void onActive()
    {
        this.gameObject.SetActive(true);

        this.CancelInvoke();
        this.Invoke("onNeedClose", BombExistDurtion);
    }

    protected override void onSleep()
    {
        this.gameObject.SetActive(false);
    }

    private void onNeedClose() {
        var ap = this.GetComponent<ActionProcesser>();
        if (ap != null)
        {
            ap.StopAllActions();
        }
        this.State = EnumItemState.E_SLEEP;
    }
}
